home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
3_12.lha
/
3_12
/
3_12.c
next >
Wrap
C/C++ Source or Header
|
1993-08-08
|
2KB
|
79 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
/ Read a token into the buffer.
/ Read a line at a time.
include <stdlib.h>
include <error.h>
oken_value get_token()
static char linebuf[512] = "";
static int lineindex = 0;
// if line is empty, fill it
if (linebuf[lineindex] == '\0')
{
if (!cin.get(linebuf, sizeof linebuf - 1))
return (curr_tok = END);
// discard newline from input
char c;
cin.get(c);
// install newline within line
int end = strlen(linebuf);
linebuf[end++] = '\n';
linebuf[end] = '\0';
lineindex = 0;
}
// delete leading whitespace on the line
while (isspace(linebuf[lineindex]) &&
(linebuf[lineindex] != '\n'))
lineindex++;
// match a token
char ch;
switch (ch = linebuf[lineindex++])
{
case ';': case '\n':
return (curr_tok = PRINT);
case '*': case '+': case '(':
case '/': case '-': case ')':
case ':':
return (curr_tok = ch);
// match a number
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
case '8': case '9': case '.':
int saveindex = lineindex - 1;
while (isdigit(ch = linebuf[lineindex]) ||
(ch == '.'))
lineindex++;
linebuf[lineindex] = '\0';
number_value = atof(&linebuf[saveindex]);
linebuf[lineindex] = ch;
return (curr_tok = NUMBER);
default:
if (isalpha(ch))
{
char *p = name_string;
for (*p++ = ch;
isalnum(ch = linebuf[lineindex]);
*p++ = ch, lineindex++)
;
*p = '\0';
return (curr_tok = NAME);
}
error("unknown token received");
return (curr_tok = PRINT);
}